package cn.quevo.cube.provide.data.validate.impl;


import cn.quevo.common.lang.StringUtils;
import cn.quevo.cube.provide.data.entity.dto.ColumnValidateDto;
import cn.quevo.cube.provide.data.validate.AbstractColumnDataValidate;
import cn.quevo.cube.provide.data.validate.ColumnDataValidate;
import cn.quevo.cube.provide.exception.ColumnCheckException;
import cn.quevo.cube.provide.exception.response.ColumnExceptionResponse;

/**
 * 文本验证
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018/12/14 6:52 PM
 * @Copyright 湘豫（北京）科技有限公司版权所有.All Rights Reserved.
 */
public class TextValidate extends AbstractColumnDataValidate implements ColumnDataValidate {
    @Override
    protected void checked(ColumnValidateDto columnValidateDto) {
        int length = StringUtils.isBlank(columnValidateDto.getValue()) ? 0 : columnValidateDto.getValue().trim().length();
        boolean isRange = (columnValidateDto.getMinLength() != null && length < columnValidateDto.getMinLength()) || (columnValidateDto.getMaxLength() != null && length > columnValidateDto.getMaxLength());
        if (!isRange) {
            throw new ColumnCheckException(ColumnExceptionResponse.LENGTH_NOT_RANGE);
        }
    }
}
